package mylittleinvestigations;

import mylittleinvestigations.CaseInformation.CaseXmlWriter;
import mylittleinvestigations.CaseInformation.Case;
import mylittleinvestigations.CaseInformation.CaseXmlReader;
import mylittleinvestigations.CaseInformation.CaseParseException;
import java.io.IOException;
import mylittleinvestigations.Events.LocationEventListener;
import mylittleinvestigations.Events.LocationEventProvider;
import org.newdawn.slick.*;

public class Area implements LocationEventListener
{
    private Location startLocation;
    private Location currentLocation;

    public Area()
    {
        this.currentLocation = null;
    }

    public Location getStartLocation()
    {
        if (this.startLocation == null)
        {
            this.startLocation = Case.getInstance().getContent().GetLocationFromId(this.getStartLocationId());
        }

        return this.startLocation;
    }

    private String id;

    public String getId() { return this.id; }
    public void setId(String id) { this.id = id; }

    private String name;

    public String getName() { return this.name; }
    public void setName(String name) { this.name = name; }

    private String startLocationId;

    public String getStartLocationId() { return this.startLocationId; }
    public void setStartLocationId(String startLocationId) { this.startLocationId = startLocationId; }

    public boolean getIsFinished()
    {
        return this.currentLocation == null;
    }

    public void Begin()
    {
        this.SwapLocations(this.getStartLocation());
    }

    public void Begin(String startLocationId)
    {
        this.SwapLocations(Case.getInstance().getContent().GetLocationFromId(startLocationId));
    }

    public void Update(GameContainer gc, int delta) throws CaseParseException
    {
        if (this.currentLocation != null)
        {
            this.currentLocation.Update(gc, delta);
        }
    }

    public void Draw(GameContainer gc, Graphics g) throws SlickException, CaseParseException
    {
        if (this.currentLocation != null)
        {
            this.currentLocation.Draw(gc, g);
        }
    }
    
    public void Reset()
    {
        this.currentLocation = null;
    }

    private void SwapLocations(Location newLocation)
    {
        String previousLocationId = null;

        if (this.currentLocation != null)
        {
            previousLocationId = this.currentLocation.getId();
            LocationEventProvider.ClearListener(this);
        }

        this.currentLocation = newLocation;

        if (this.currentLocation != null)
        {
            this.currentLocation.Begin(previousLocationId);
            LocationEventProvider.RegisterListener(this);
        }
    }

    public void OnLocationExited(Location sender, Location targetLocation)
    {
        if (sender == this.currentLocation)
        {
            this.SwapLocations(targetLocation);
        }
    }

    public void SaveToXml(CaseXmlWriter writer) throws IOException
    {
        writer.StartElement("Area");
        writer.WriteTextElement("Id", this.id);
        writer.WriteTextElement("Name", this.name);
        writer.WriteTextElement("StartLocationId", this.startLocationId);
        writer.EndElement();
    }

    public Area(CaseXmlReader reader)
    {
        this.currentLocation = null;
        
        reader.StartElement("Area");
        this.id = reader.ReadTextElement("Id");
        this.name = reader.ReadTextElement("Name");
        this.startLocationId = reader.ReadTextElement("StartLocationId");
        reader.EndElement();
    }
}
